package com.block.source.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.db.entity.*;
import com.block.db.mapper.SourceMemberDataInfoMapper;
import com.block.source.model.param.UserDataParam;
import com.block.source.service.ISourceMemberDataInfoService;
import com.block.source.service.ISystemIpAddressInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 线索用户资质信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-12-25
 */
@Service
public class SourceMemberDataInfoServiceImpl extends ServiceImpl<SourceMemberDataInfoMapper, SourceMemberDataInfo> implements ISourceMemberDataInfoService {

    @Autowired
    ISystemIpAddressInfoService systemIpAddressInfoService;

    @Override
    public SourceMemberDataInfo saveMemberData(SourceMemberDataInfoTemp memberDataInfoTemp, UserDataParam userDataParam, MemberBaseInfo memberBaseInfo) {

        SourceMemberDataInfo memberDataInfo  = new SourceMemberDataInfo();
        BeanUtil.copyProperties(memberDataInfoTemp, memberDataInfo, true);

        memberDataInfo.setUid(memberBaseInfo.getUid());
        memberDataInfo.setDateInfo(LocalDate.now().toString());
        memberDataInfo.setMobile(userDataParam.getMobile());
        memberDataInfo.setRedirectUrl(userDataParam.getRedirectUrl());

        boolean save = this.save(memberDataInfo);
        if (save){
            return memberDataInfo;
        }
        return null;
    }

    @Async
    @Override
    public void updateIpInfo(Long id) {

        try {
            // 更新该记录的 ip
            SourceMemberDataInfo memberDataInfo = this.getById(id);
            if (StrUtil.isEmpty(memberDataInfo.getUserIp())
                    || StrUtil.equals("127.0.0.1", memberDataInfo.getUserIp())
                    || StrUtil.equals("0.0.0.0", memberDataInfo.getUserIp()) ){
                return;
            }

            if (ObjectUtil.isNotEmpty(memberDataInfo.getSameFlag()) && ObjectUtil.isNotEmpty(memberDataInfo.getIpAddress())){
                return;
            }

            //
            SystemIpAddressInfo ipAddressInfo = systemIpAddressInfoService.getIpAddressInfo(memberDataInfo.getUserIp());
            if (ObjectUtil.isNotEmpty(ipAddressInfo)){
                SourceMemberDataInfo updateEntity = new SourceMemberDataInfo();
                updateEntity.setId(id);
                updateEntity.setIpAddress(ipAddressInfo.getAddressInfo());

                if (StrUtil.isSubEquals(ipAddressInfo.getCodeInfo(),0,  memberDataInfo.getCityCode(), 0 ,4, true)){
                    updateEntity.setSameFlag(1);
                }else{
                    updateEntity.setSameFlag(0);
                }
                this.updateById(updateEntity);
            }
        }catch (Exception exception){
            exception.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Integer asdf=1;
        String asfdazx = "234";
        System.out.println(ObjectUtil.isAllNotEmpty(asdf,asfdazx));
        System.out.println(ObjectUtil.isNotEmpty(asdf) && ObjectUtil.isNotEmpty(asfdazx));

        String asfdazx1 = "2342aa";
        String asfdazx2 = "2344bb";
        if (StrUtil.isSubEquals(asfdazx1,0,  asfdazx2, 0 ,4, true)){
            System.out.println(1);
        }else{
            System.out.println(0);
        }
    }

}
